﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using ViewModels;

namespace LibraryMIS_SE01.UC
{
    public partial class BookManagement : UserControl
    {
        private BookBLL bll;//定义书籍业务逻辑访问类
        private DataSet ds;//定义数据集对象，目的是用于填充列表区域
        private string Operation = "Insert";//标识操作类型。Insert：新增；Update：更新
        private string selectedBookID = String.Empty;//定义被选行的索书号

        public BookManagement()
        {
            InitializeComponent();
            bll = new BookBLL();//实例化业务逻辑对象
        }
        
        /// 清除文本输入框
        private void TextBoxClean()
        {
            foreach(Control control in this.Controls)
            {
                if (control is TextBox)
                    ((TextBox)control).Clear();
            }
        }
        /// “撤销”按钮实现
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            //TextBoxClean();
            this.textBoxBookID.Clear();
            this.textBoxBookName.Clear();
            this.textBoxISBN.Clear();
            this.textBoxAuthor.Clear();
            this.textBoxBookVersion.Clear();
            this.textBoxPublisher.Clear();
            this.textBoxWordCount.Clear();
            this.textBoxPageCount.Clear();
            this.textBoxPrice.Clear();
        }
        private void GetBookList()
        {
            ds = bll.GetBookList();
            dataGridViewBookList.DataSource = ds.Tables[0];//将数据集绑定到列表控件中
        }
        private void BookManagement_Load(object sender, EventArgs e)
        {
            GetBookList();
            BookClassDropDown(comboBoxBookClass);//加载默认下拉菜单内容
        }
        private void UCBookbtnSearch_Click(object sender, EventArgs e)
        {
            string inputString = this.UCBooktxtSearchStr.Text.Trim();//捕获用户输入搜索内容，并且清空空格（Trim）

            if (String.IsNullOrEmpty(inputString))//如果搜索内容为空
            {
                MessageBox.Show("请输入搜索内容！","系统提示");
                this.UCBooktxtSearchStr.Focus();//定位光标位置
                return;
            }
            //如果未选择下拉内容
            if(this.UCBookcbSelected.SelectedIndex == -1)
            {
                MessageBox.Show("请进行下拉内容选择！", "系统提示");
                return;
            }

            if(this.UCBookcbSelected.SelectedIndex==0)//根据BookID搜索
            {
                ds = bll.GetBookListByBookID(inputString);
                dataGridViewBookList.DataSource = ds.Tables[0];//将数据集绑定到列表控件中
            }
            if (this.UCBookcbSelected.SelectedIndex == 1)//根据BookName搜索
            {
                ds = bll.GetBookListByBookName(inputString);
                dataGridViewBookList.DataSource = ds.Tables[0];//将数据集绑定到列表控件中
            }
            if (this.UCBookcbSelected.SelectedIndex == 2)//根据BookName搜索
            {
                ds = bll.GetBookListByPublisher(inputString);
                dataGridViewBookList.DataSource = ds.Tables[0];//将数据集绑定到列表控件中
            }
            if (this.UCBookcbSelected.SelectedIndex == 3)//根据BookName搜索
            {
                ds = bll.GetBookListByAuthor(inputString);
                dataGridViewBookList.DataSource = ds.Tables[0];//将数据集绑定到列表控件中
            }
        }
        private void BookClassDropDown(ComboBox control)
        {
            DataSet classList = bll.GetBookClassList();
            control.DataSource = classList.Tables[0];//绑定下拉菜单数据源
            control.DisplayMember = "BookClass";//指定下拉菜单显示字段
            control.ValueMember = "ClassID";//指定下拉菜单的存储值
        }
        private void comboBoxBookClass_VisibleChanged(object sender, EventArgs e)
        {
            try
            {
                BookClassDropDown(comboBoxBookClass);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示");
            }
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            // 将用户输入内容捕获，完成BookVM成员的赋值
            BookVM vm = new BookVM()
            {
                BookID = this.textBoxBookID.Text.Trim(),//Trim()去除字符串中的空格
                BookName = this.textBoxBookName.Text.Trim(),
                ISBN = this.textBoxISBN.Text.Trim(),
                Author = this.textBoxAuthor.Text.Trim(),
                BookVersion=this.textBoxBookVersion.Text.Trim(),
                WordCount=int.Parse(this.textBoxWordCount.Text.Trim()),
                PageCount = int.Parse(this.textBoxPageCount.Text.Trim()),
                Publisher=this.textBoxPublisher.Text.Trim(),
                Price=decimal.Parse(this.textBoxPrice.Text.Trim()),

                PublishDate= dateTimePickerPublishDate.Value,//获取出版日期
            };
            // 获取图书类别下拉菜单选定的ClassID值
            vm.BookClassesVM = new BookClassesVM()
            {
                ClassID = this.comboBoxBookClass.SelectedValue.ToString().Trim()
            };
            //调用BookBLL中的方法完成新增、修改操作
            object result = new object();
            if(buttonAdd.Text=="新增" && Operation=="Insert")
            {
                result = bll.EditBook(vm, Operation);
                if (result.ToString().Equals("库中已有该书籍记录，请勿重复插入！"))
                {
                    MessageBox.Show(result.ToString(), "系统提示");
                    return;
                }
                if(int.Parse(result.ToString())>0)
                    MessageBox.Show("成功新增数据！", "系统提示");
                else
                    MessageBox.Show("新增数据失败！", "系统提示");
            }

            if (buttonAdd.Text == "修改" && Operation == "Update")
            {
                result = bll.EditBook(vm, Operation);
                if (int.Parse(result.ToString()) > 0)
                {
                    MessageBox.Show("成功修改数据！", "系统提示");
                    GetBookList();
                }
                else
                    MessageBox.Show("修改数据失败！", "系统提示");
            }
        }

        private void dataGridViewBookList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > 0)
            {
                selectedBookID = dataGridViewBookList.Rows[e.RowIndex].Cells[0].Value.ToString();//当前被选行的索书号

                ds = bll.GetBookByBookID(selectedBookID);

                if (ds.Tables[0].Rows.Count > 0)//判断数据集是否有返回记录
                {
                    textBoxBookID.Text = ds.Tables[0].Rows[0][0].ToString();
                    textBoxBookName.Text = ds.Tables[0].Rows[0][1].ToString();
                    textBoxISBN.Text = ds.Tables[0].Rows[0][2].ToString();
                    textBoxAuthor.Text = ds.Tables[0].Rows[0][3].ToString();

                    dateTimePickerPublishDate.CustomFormat = ds.Tables[0].Rows[0][4].ToString();
                    dateTimePickerPublishDate.Format = DateTimePickerFormat.Custom;

                    textBoxBookVersion.Text = ds.Tables[0].Rows[0][5].ToString();
                    textBoxWordCount.Text = ds.Tables[0].Rows[0][6].ToString();
                    textBoxPageCount.Text = ds.Tables[0].Rows[0][7].ToString();
                    textBoxPublisher.Text = ds.Tables[0].Rows[0][8].ToString();
                    textBoxPrice.Text = ds.Tables[0].Rows[0][9].ToString();

                    comboBoxBookClass.Text = ds.Tables[0].Rows[0][10].ToString();

                    buttonAdd.Text = "修改";
                    Operation = "Update";
                    buttonAdd.BackColor = Color.Blue;
                }
            }
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            try
            {
                bool result=bll.DelBookByID(selectedBookID);
                if (result)
                {
                    MessageBox.Show("删除数据成功！", "系统提示");
                    GetBookList();//重新加载数据列表
                }
                else
                {
                    MessageBox.Show("删除数据失败！", "系统提示");
                }

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
